package com.medical_system.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.medical_system.dao.SickDao;
import com.medical_system.entity.DoctorA;
import com.medical_system.entity.KeShi;
import com.medical_system.entity.Sick;
import com.medical_system.util.JDBCUtil;

public class SickDaoImpl implements SickDao {
	JDBCUtil util = JDBCUtil.getInit();

	@Override
	public Sick queryByID(int id) {
		ResultSet rs = util.query("SELECT * from sick s INNER JOIN ks k  where k.ks_id=s.ks_chose and s.ks_chose=?",
				id);
		try {
			if (rs.next()) {
				Sick s = new Sick();
				s.setSickId(rs.getInt("sick_id"));
				s.setSickName(rs.getString("sick_name"));
				s.setSickPhone(rs.getString("sick_phone"));
				s.setSickCity(rs.getString("sick_city"));
				s.setSickSex(rs.getString("sick_sex").charAt(0));
				KeShi ks = new KeShi();
				ks.setKsid(rs.getInt("ks_id"));
				ks.setKsname(rs.getString("ks_name"));
				s.setKs(ks);
				return s;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			util.closeAll();
		}

		return null;
	}

	@Override
	public Sick queryByName(String name) {
		ResultSet rs = util.query("SELECT * from sick s INNER JOIN ks k  where k.ks_id=s.ks_chose and s.sick_name=?",
				name);
		try {
			if (rs.next()) {
				Sick s = new Sick();
				s.setSickId(rs.getInt("sick_id"));
				s.setSickName(rs.getString("sick_name"));
				s.setSickPhone(rs.getString("sick_phone"));
				s.setSickCity(rs.getString("sick_city"));
				s.setSickSex(rs.getString("sick_sex").charAt(0));
				KeShi ks = new KeShi();
				ks.setKsid(rs.getInt("ks_id"));
				ks.setKsname(rs.getString("ks_name"));
				s.setKs(ks);
				return s;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			util.closeAll();
		}

		return null;
	}

	@Override
	public List<Sick> queryAll() {
		ResultSet rs = util.query("SELECT * from sick s INNER JOIN ks k  where k.ks_id=s.ks_chose");
		List<Sick> list = new ArrayList<Sick>();
		try {
			while (rs.next()) {
				Sick s = new Sick();
				s.setSickId(rs.getInt("sick_id"));
				s.setSickName(rs.getString("sick_name"));
				s.setSickPhone(rs.getString("sick_phone"));
				s.setSickCity(rs.getString("sick_city"));
				s.setSickSex(rs.getString("sick_sex").charAt(0));
				KeShi ks = new KeShi();
				ks.setKsid(rs.getInt("ks_id"));
				ks.setKsname(rs.getString("ks_name"));
				s.setKs(ks);
				list.add(s);

			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			util.closeAll();
		}

		return list;
	}

}
